package com.yj.discovery.mapper;

import com.yj.discovery.entity.DiscoveryItem;
import com.yj.inventory.entity.Inventory;
import com.yj.transport.entity.TransportInfo;
import com.yj.transport.entity.TransportJournal;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;

/**
 * Copyright 2017 (C) Yunjian-VC
 * Created on : 2017/12/13
 * Author     : Unbelievable9
 **/
@Repository
@Mapper
public interface DiscoveryMapper {

    @Select("SELECT transport_type, longitude, latitude FROM transport_info ORDER BY update_time LIMIT #{limit}")
    List<DiscoveryItem> getItemByLimit(@Param("limit") Integer limit);

    @Select("SELECT COUNT(*) FROM transport_info WHERE transport_type = #{type}")
    Integer totalCountForTransportType(@Param("type") Integer type);

    @Select("SELECT SUM(CAST(contract_amount AS DECIMAL(20,0))) FROM contract WHERE contract_status = 1")
    BigDecimal totalCoalAmountForContract();

    @Select("SELECT SUM(goods_weight) FROM transport_info WHERE transport_status = #{status}")
    BigDecimal totalCoalAmountForTransportStatus(@Param("status") Integer status);

    @Select("SELECT COUNT(*) FROM inventory WHERE type = #{type}")
    Integer countForInventoryByType(@Param("type") Integer type);

    @Select("SELECT SUM(amount) FROM inventory WHERE type = #{type}")
    BigDecimal totalInventoryAmountForType(@Param("type") Integer type);

    @Select("SELECT * FROM transport_info ORDER BY create_time DESC LIMIT #{limit}")
    List<TransportInfo> getTransportInfoByLimit(@Param("limit") Integer limit);

    @Select("<script>" +
            "SELECT * FROM transport_journal " +
            "WHERE transport_info_id IN " +
            "<foreach item = 'item' collection = 'list' open = '(' separator = ', ' close = ') '>" +
            "#{item}" +
            "</foreach>" +
            "ORDER BY transport_info_id DESC,arrive_time asc" +
            "</script>")
    List<TransportJournal> getTransportJournal(@Param("list") List<Integer> list);

    @Select("SELECT * FROM inventory ORDER BY create_time DESC LIMIT #{limit}")
    List<Inventory> getAllInventory(@Param("limit") Integer limit);
}
